package org.lmind.example.uc.webapp.controller;

import javax.annotation.Resource;
import javax.ejb.TransactionAttribute;

import org.lmind.example.common.api.auth.AuthenticationService;
import org.lmind.example.common.api.auth.Certificate;
import org.lmind.example.common.api.auth.NamePasswordCredential;
import org.lmind.example.common.web.AbstractController;
import org.lmind.example.uc.api.UserCenter;
import org.lmind.example.uc.webapp.view.PageView;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "user")
public class LoginPage extends AbstractController {
	
	@Resource
	private UserCenter userCenter;
	
	@Resource
	private AuthenticationService authenticationService;

	@RequestMapping(value = "login", method = RequestMethod.GET)
	public Object page() {
		
		TransactionAttribute.class.getName();
		userCenter.login("11", "22");
		return new PageView("index", "hello world");
	}
	
	public void login(String name, String password) {
		NamePasswordCredential credential = new NamePasswordCredential();
		credential.setName(name);
		credential.setPassword(password);
		Certificate cert = authenticationService.authenticate(credential);
		
		sessionContext.setCertificate(cert);
	}
}
